package com.ruoyi.liquibase;

import liquibase.integration.spring.SpringLiquibase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * @version V1.0
 * @Package com.ruoyi.framework.config
 * @ClassName liquibaseConfig
 * @Description TODO
 * @Author 王振鹏
 * @date 2021/11/23 0:12
 **/
@Configuration
public class liquibaseConfig {



    /**
     * 实现 liquibase在多数据源创建表结构
     * liquibase配置
     * */
    @Bean(name = "adminLiquibaseProperties")
    @ConfigurationProperties(prefix = "spring.datasource.druid.master.liquibase")
    public LiquibaseProperties adminLiquibaseProperties() {
        System.out.println("--------------------------------------------");
        System.out.println(new LiquibaseProperties().getChangeLog());
        return new LiquibaseProperties();
    }



    @Bean(name = "adminLiquibase")
    public SpringLiquibase adminLiquibase(DataSource dataSource) {
        return springLiquibase(dataSource, adminLiquibaseProperties());
    }

    private static SpringLiquibase springLiquibase(DataSource dataSource, LiquibaseProperties properties) {
        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog(properties.getChangeLog());
        liquibase.setContexts(properties.getContexts());
        liquibase.setDefaultSchema(properties.getDefaultSchema());
        liquibase.setDropFirst(properties.isDropFirst());
        liquibase.setShouldRun(properties.isEnabled());
        liquibase.setLabels(properties.getLabels());
        liquibase.setChangeLogParameters(properties.getParameters());
        liquibase.setRollbackFile(properties.getRollbackFile());
        return liquibase;
    }
}
